<%@ codepage=65001%><% 'UTF-8%>

<% '商品管理%>
<!--#include file="../Include/Common.All.asp"-->
<!--#include file="../Include/Conn.asp"-->
<% call OpenConn%>

<!--#include file="../Include/Common.Security.asp"-->
<!--#include file="../Include/MD5.asp"-->
<!--#include file="../Include/JSON_2.0.2.asp"-->
<!--#include file="../Include/JSON_UTIL_0.1.1.asp"-->



<%

	'定义json对象
	Dim jsonObj,jsonObj_user
	Set jsonObj = jsObject()
	Set jsonObj_user = jsObject()
	UserID=ReplaceBadChar(Trim(Session("UserID")))
	
	dim act 
  act=request("act")
 	Dim rsObj,strSql
 	''定义查询字段变量
 	dim id,role_id,role_name
 	''影响的记录条数
 	Dim  RowCount 
	''查询信息列表
	if "search"=act then
		role_name=ReplaceBadChar(Trim(request.Form("role_name")))
				
		jsonObj("rslist") = ""
		''添加用户信息
		jsonObj_user("UserID") = UserID 
		jsonObj_user("UserName") =ReplaceBadChar(Trim(Session("UserName")))
		Set jsonObj("customer") = jsonObj_user 
	 
		''添加查询结果信息，按照菜单排序
		''Dim rsObj,strSql 
		strSql = "SELECT id, role_name  "
		strSql=strSql & "  FROM T_ROLE "   & "WHERE 1=1 "
		If (role_name <> "") Then strSql=strSql & " and role_name like '%" & role_name & "%'  "
		strSql=strSql & " ORDER BY id  "
		Set jsonObj("rslist") = QueryToJSON(Conn, strSql)
		
 	elseif "add"=act then
 			jsonObj("msg") = "&#x64CD;&#x4F5C;&#x5931;&#x8D25;&#xFF0C;&#x8BF7;&#x91CD;&#x8BD5;..."'''' "操作失败，请重试..."
			jsonObj("success") = 0'"false"

''添加信息
			role_id=ReplaceBadChar(Trim(request.Form("role_id")))
			role_name=ReplaceBadChar(Trim(request.Form("role_name")))
 
			''Dim rsObj,strSql
			set rsObj=Server.CreateObject("ADODB.Recordset")
			strSql = "Select  * from T_ROLE where id='"&role_id&"' "
			''Response.Write strSql&"-----"
			rsObj.Open strSql, Conn, 1, 3
			If rsObj.BOF And rsObj.EOF Then 
				rsObj.AddNew '如果没记录新增，有记录update	
				strSql = "select ISNULL(MAX(id), 0)  from T_ROLE  "
				role_id = PE_CLng(Conn.Execute(strSql)(0)) + 1
			end if				
			rsObj("id") = role_id
			rsObj("role_name") = role_name
			rsObj.Update
			rsObj.close
			Set rsObj = Nothing
		 
			jsonObj("msg") = "&#x6DFB;&#x52A0;&#x6210;&#x529F;&#xFF0C;&#x6B63;&#x5728;&#x8DF3;&#x8F6C;..." ''' "添加成功，正在跳转..."
			jsonObj("success") = 1'"success"
			jsonObj("url") = "roles.asp"
	 	 		
 elseif "edit"=act then
 			jsonObj("msg") = "&#x64CD;&#x4F5C;&#x5931;&#x8D25;&#xFF0C;&#x8BF7;&#x91CD;&#x8BD5;..." '''"操作失败，请重试..."
			jsonObj("success") = 0'"false"
			
 
''修改信息
			role_id=ReplaceBadChar(Trim(request.Form("role_id")))
			role_name=ReplaceBadChar(Trim(request.Form("role_name")))
			role_res_id_all=ReplaceBadChar(Trim(request.Form("id_all"))) ''res列表
			
 
			''Dim rsObj,strSql
			set rsObj=Server.CreateObject("ADODB.Recordset")
			strSql = "Select  * from T_ROLE where id='"&role_id&"' "
			''Response.Write strSql&"-----"
			rsObj.Open strSql, Conn, 1, 3
			If rsObj.BOF And rsObj.EOF Then 
				jsonObj("msg") = "&#x64CD;&#x4F5C;&#x5931;&#x8D25;&#xFF0C;&#x8BF7;&#x91CD;&#x8BD5;..." ''' "操作失败，请重试..."
				jsonObj("success") = 0'"false"
			else				
				'rsObj("id") = role_id
				rsObj("role_name") = role_name
				rsObj.Update
				rsObj.close
				
				''删除该用户原有的角色，添加现有已经选中的角色
			  RowCount = 0
		    Conn.Execute ("delete from T_ROLE_RES where role_id='"&role_id&"'"), RowCount		    	
		    ''添加选中的角色
				strSql =" INSERT INTO T_ROLE_RES ( res_id,role_id) "
				strSql =strSql & " SELECT id, "&role_id&" AS role_res_id "
				strSql =strSql & " FROM T_RES WHERE 1=2 "
				If ("" <> role_res_id_all ) Then  strSql =strSql & " or id IN ("&role_res_id_all&")  "
		    Conn.Execute (strSql), RowCount		 
			 
				jsonObj("msg") = "&#x6DFB;&#x52A0;&#x6210;&#x529F;&#xFF0C;&#x6B63;&#x5728;&#x8DF3;&#x8F6C;..."'' "添加成功，正在跳转..."
				jsonObj("success") = 1'"success"
				jsonObj("url") = "roles.asp"
	 		end if	

			Set rsObj = Nothing
 
 elseif "delete"=act then
''删除记录
		id=ReplaceBadChar(Trim(request.Form("id"))) 
    
    RowCount = 0
    strSql = "select count(id)  from T_USER_ROLE where  role_id ='"&id&"'"
		RowCount = PE_CLng(Conn.Execute(strSql)(0)) 
		if 0<RowCount then 
			  jsonObj("success") = 0 'false
        jsonObj("msg") = "记录被引用，删除失败."  
		else
					
	    If (id <> "" ) Then 
	    	Conn.Execute ("delete from T_ROLE where id='"&id&"'"), RowCount
	    End If
	    If RowCount = 0 Then
	        jsonObj("success") = 0 'false
	        jsonObj("msg") = "记录删除失败." ''"&#x8BB0;&#x5F55;&#x5220;&#x9664;&#x5931;&#x8D25;." '''' 
	    Else
	        jsonObj("msg") = 	"删除成功，正在跳转..." '''"&#x5220;&#x9664;&#x6210;&#x529F;&#xFF0C;&#x6B63;&#x5728;&#x8DF3;&#x8F6C;..." ''''
					jsonObj("success") = 1'"success"
					jsonObj("url") = "roles.asp"
	    End If
    End If
  

	end If''''删除记录
 

	'输出json对象
	jsonObj.Flush
	''Response.Write strSql&"-----"
 '释放数据库链接
	call CloseConn

''{
''    rslist : [ { name: "mac", desc: "computer",     
''                   price: 1000, quantity: 100, alert:null },
''                 { name: "ipod", desc: "music player", 
''                   price:  200, quantity: 200, alert:"on sale now!" },
''                 { name: "cinema display", desc: "screen",       
''                   price:  800, quantity: 300, alert:"best deal!" } ],
''    customer : { first: "John", last: "Public", level: "gold" }
''}

%>